package com.hxzy.service.impl;

import com.hxzy.dto.UnitDTO;
import com.hxzy.entity.Unit;
import com.hxzy.mapper.UnitMapper;
import com.hxzy.service.UnitService;
import com.hxzy.util.MybatisUtil;
import com.hxzy.vo.UnitAndBuildVo;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lqq
 * @Description:
 * @Date: 2022/3/8 13:45
 */
public class UnitServiceImpl implements UnitService {
    @Override
    public int insert(Unit unit) {
        int count=0;
        try(SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
           count= mapper.insert(unit);
           sqlSession.commit();
        }
        return count;
    }

    @Override
    public int update(Unit unit) {
        int count=0;
        try(SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
            count= mapper.update(unit);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public int delete(String id) {
        int count=0;
        try(SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
            count= mapper.delete(id);
            sqlSession.commit();
        }
        return count;
    }

    @Override
    public Unit findByONe(String id) {
        Unit  unit=null;
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
            unit = mapper.findByONe(id);
        }
        return unit;
    }

    @Override
    public List<UnitAndBuildVo> search(UnitDTO unitDTO) {
        List<UnitAndBuildVo> list=new ArrayList<>();
        try(SqlSession sqlSession=MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
            list=mapper.search(unitDTO);
        }
        return list;
    }

    @Override
    public int findById(Unit unit) {
        int count=0;
        try(SqlSession sqlSession= MybatisUtil.getSqlSessionFactory().openSession()){
            UnitMapper mapper = sqlSession.getMapper(UnitMapper.class);
            count= mapper.findById(unit);

        }
        return count;
    }
}
